﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using System.Text;
using System.Collections.Generic;

public partial class Task_SearchTask : System.Web.UI.Page
{
    //设置每页行数
    private int pageSize = 20;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFirstList();
            BindSecondList();
            BindTypeList();
            StartDate.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToShortDateString();
            EndDate.Text = DateTime.Now.ToShortDateString();
            BindGrid(0);
        }
    }
    private void BindFirstList()
    {
        FirstList.Items.Clear();
        ListItem li = new ListItem();
        li.Text = "";
        li.Value = "";
        FirstList.Items.Add(li);

        string sql = "SELECT * FROM Account_Departments WHERE (SUBSTRING(DepartmentID, 3, 2) = '00') AND (SUBSTRING(DepartmentID, 5, 1) = '0') AND (SUBSTRING(DepartmentID, 7, 1) = '0') AND (SUBSTRING(DepartmentID, 6, 1) != '0')";
        DataView dv = SplitPage.GetPageData(sql, 0, 1000);
        foreach (DataRowView drv in dv)
        {
            li = new ListItem();
            li.Text = drv["ShortName"].ToString();
            li.Value = drv["DepartmentID"].ToString();
            FirstList.Items.Add(li);
        }
    }
    private void BindSecondList()
    {
        SecondList.Items.Clear();
        ListItem li = new ListItem();
        li.Text = "";
        li.Value = "";
        SecondList.Items.Add(li);

        string sql;
        if (FirstList.SelectedValue == "")
        {
            sql = "SELECT * FROM Account_Departments WHERE (SUBSTRING(DepartmentID, 3, 2) = '00') AND (SUBSTRING(DepartmentID, 5, 1) = '0') AND (SUBSTRING(DepartmentID, 7, 1) != '0')";
        }
        else
        {
            sql = "SELECT * FROM Account_Departments WHERE (SUBSTRING(DepartmentID, 3, 2) = '00') AND (SUBSTRING(DepartmentID, 5, 1) = '0') AND (SUBSTRING(DepartmentID, 7, 1) != '0') AND (SUBSTRING(DepartmentID, 6, 1) = '" + FirstList.SelectedValue.Substring(5, 1) + "')";
        }
        DataView dv = SplitPage.GetPageData(sql, 0, 1000);
        foreach (DataRowView drv in dv)
        {
            li = new ListItem();
            li.Text = drv["ShortName"].ToString();
            li.Value = drv["DepartmentID"].ToString();
            SecondList.Items.Add(li);
        }
    }
    private void BindTypeList()
    {
        TypeList.Items.Clear();
        ListItem li = new ListItem();
        li.Text = "";
        li.Value = "";
        TypeList.Items.Add(li);

        List<TaskTypeInfo> types = TaskType.GetTaskTypes();
        foreach (TaskTypeInfo type in types)
        {
            li = new ListItem();
            li.Text = type.TypeName;
            li.Value = type.TypeId.ToString();
            TypeList.Items.Add(li);
        }
        TypeList.SelectedIndex = 0;
    }
    private void BindGrid(int pageIndex)
    {
        ViewState["PageIndex"] = pageIndex;
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append(" Select * From Tasks Where ParentId=0");
        selectCommandText.Append(" and ((StartDate>='" + StartDate.Text + "' and StartDate<='" + EndDate.Text + "')");
        selectCommandText.Append(" or (StartDate<'"+EndDate.Text+"' and EndDate>='"+EndDate.Text+"'))");
        if (FirstList.SelectedValue != "")
        {
            selectCommandText.Append(" and Left(CreateDepartment,6)='" + FirstList.SelectedValue.Substring(0, 6) + "'");
        }
        if (SecondList.SelectedValue != "")
        {
            selectCommandText.Append(" and CreateDepartment='" + SecondList.SelectedValue + "'");
        }
        if (TypeList.SelectedValue != "")
        {
            selectCommandText.Append(" and TaskType=" + TypeList.SelectedValue);
        }
        UserGrid.DataSource = SplitPage.GetPageData(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        UserGrid.DataBind();

        int totalCount = SplitPage.GetRowCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        ViewState["PageCount"] = (totalCount % pageSize) == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ViewState["PageIndex"].ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 1;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 1;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"];
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"];
        BindJumpList();
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"];
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * pageSize + (e.Row.RowIndex + 1)).ToString();
        }
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(int.Parse(JumpList.SelectedValue));
    }
    public string GetTaskTypeName(int taskType)
    {
        return TaskType.GetTaskType(taskType).TypeName.Substring(0, 1);
    }
    public string GetCreateDepartmentName(string departmentId)
    {
        return Department.GetDepartment(departmentId).ShortName;
    }
    public string GetTitle(string title)
    {
        return title.Length > 52 ? title.Substring(0, 52) + "..." : title;
    }
    protected void FirstList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSecondList();
        BindGrid(0);
    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        BindGrid(0);
    }
    protected void SecondList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(0);
    }
    protected void TypeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(0);
    }
}
